
/*
    全局测试套件
    在整体的测试中，只会初始化一次环境，在所有测试用例完毕后，才会清理环境
*/

#include <iostream>
#include <unordered_map>
#include <gtest/gtest.h>

// 定义全局测试套件
class MyEnvironment : public testing::Environment
{
public:
    void SetUp() override
    {
        std::cout << "开始初始化测试环境" << std::endl;
    }
    void TearDown() override
    {
        std::cout << "清理测试环境" << std::endl;
    }
};
// 单元测试通常与测试套件名称相同
TEST(MyEnvironment, test1)
{
    std::cout << "单元测试1\n"
              << std::endl;
}
TEST(MyEnvironment, test2)
{
    std::cout << "单元测试2\n"
              << std::endl;
}

std::unordered_map<std::string,std::string> mymap;
class MyMapTest : public testing::Environment
{
public:
    void SetUp() override
    {
        std::cout << "开始初始化测试环境map" << std::endl;
        mymap.insert(std::make_pair("111","222"));
        mymap.insert(std::make_pair("333","444"));
    }
    void TearDown() override
    {
        std::cout << "清理测试环境map" << std::endl;
        mymap.clear();
    }
};
TEST(MyMapTest, test1)
{
    ASSERT_EQ(mymap.size(),2);
    std::cout << "单元测试1\n"
              << std::endl;
    mymap.erase("111");
}
TEST(MyMapTestt, test2)
{
    ASSERT_EQ(mymap.size(),2);
    std::cout << "单元测试2\n"
              << std::endl;
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    // 添加全局测试套件
    testing::AddGlobalTestEnvironment(new MyEnvironment);
    testing::AddGlobalTestEnvironment(new MyMapTest);
    int ret = RUN_ALL_TESTS();

    return 0;
}